package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.AppVariable;
import com.example.demo.common.PasswordUtils;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.Userinfo;
import com.example.demo.entity.vo.UserinfoVO;
import com.example.demo.service.ArticleService;
import com.example.demo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserService userService;
    @RequestMapping("/reg")
    public AjaxResult reg(Userinfo userinfo){
        //非空校验和参数有效性校验
        if(userinfo==null || !StringUtils.hasLength(userinfo.getUsername()) ||
                !StringUtils.hasLength(userinfo.getPassword())){
            return AjaxResult.fail(-1,"非法参数");
        }
        //密码加盐
        userinfo.setPassword(PasswordUtils.encrypt(userinfo.getPassword()));
        return AjaxResult.success(userService.reg(userinfo));
    }
    @RequestMapping("/login")
    public AjaxResult login(HttpServletRequest request,String username, String password){
        //1.非空校验
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            return AjaxResult.fail(-1,"非法请求");
        }
        //2.查询数据库
        Userinfo userinfo=userService.getUserByName(username);
        if(userinfo!=null && userinfo.getId()>0){
            //有效的用户名
            //密码是否正确
            if(PasswordUtils.check(password,userinfo.getPassword())){
                //登录成功
                //todo:将用户存储到session中
                HttpSession session=request.getSession();
                session.setAttribute(AppVariable.USER_SESSION_KEY,userinfo);
                userinfo.setPassword(""); //返回前端之前，隐藏敏感信息
                return AjaxResult.success(userinfo);
            }
        }
        return AjaxResult.success(null,0);
    }
    @RequestMapping("/showinfo")
    public AjaxResult showInfo(HttpServletRequest request){
        UserinfoVO userinfoVO=new UserinfoVO();
        //1.得到当前登陆用户(从session中获取)
        Userinfo userinfo= UserSessionUtils.getSessionUser(request);
        if(userinfo==null){
            return AjaxResult.fail(-1,"非法请求");
        }
        //Spring提供的深克隆方法
        BeanUtils.copyProperties(userinfo,userinfoVO);
        //2.得到用户发表文章的总数
        userinfoVO.setArtCount(articleService.getArtCountByUid(userinfo.getId()));

        return AjaxResult.success(userinfoVO);
    }
    /*
    * 注销功能的实现
    * */
    @RequestMapping("/logout")
    public AjaxResult logout(HttpSession session){
        session.removeAttribute(AppVariable.USER_SESSION_KEY);
        return AjaxResult.success("1");
    }

    @RequestMapping("/getuserbyid")
    public AjaxResult getUserById(Integer id){
        if(id==null || id<=0){
            //无效参数
            return AjaxResult.fail(-1,"非法参数");
        }
        Userinfo userinfo=userService.getUserById(id);
        if(userinfo==null || userinfo.getId()<=0){
            return AjaxResult.fail(-1,"无效参数");
        }
        //去除敏感数据
        userinfo.setPassword("");
        //查询当前用户发表的文章数
        UserinfoVO userinfoVO=new UserinfoVO();
        BeanUtils.copyProperties(userinfo,userinfoVO);
        userinfoVO.setArtCount(articleService.getArtCountByUid(id));
        return AjaxResult.success(userinfoVO);
    }
}
